package multithread.CountDown;

import java.util.concurrent.CountDownLatch;

/**
 * Created by xi on 2018/7/15.
 * Demo1 测试多个线程一起启动 .让子线程都阻塞
 */
public class CountDownLatchTestSubThreadSameTimeRun {
    @SuppressWarnings("unchecked")
    public static void main(String[] args) throws InterruptedException {
        CountDownLatchTestSubThreadSameTimeRun test = new CountDownLatchTestSubThreadSameTimeRun();
        int threadNum = 5;
        CountDownLatch latch = new CountDownLatch(threadNum);
        for(int i=0;i<threadNum;i++){
            Thread t1 = new Thread(test.new MapOper(latch));
            t1.setName("Thread:"+i);
            t1.join();
            t1.start();
            Thread.sleep(1000);
            latch.countDown();
            System.out.println("主线程：" + Thread.currentThread().getName() + ",countdown:"+i);
        }
    }

    public class MapOper implements Runnable {

        CountDownLatch latch;

        public MapOper(CountDownLatch latch) {
            this.latch = latch;
        }

        public void run() {
            try {
                System.out.println("子线程："+Thread.currentThread().getName()+",等待");
                latch.await();
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            System.out.println("子线程："+Thread.currentThread().getName()+",开始执行");
           // PhoneNumberUtilTest.test("0000000000000000");//63% 000000000000000
        }
    }

}